package com.ruge.system.service.impl;

import com.ruge.system.pojo.entity.SysLogInfo;
import com.ruge.system.pojo.entity.SysMenu;
import com.ruge.system.repository.SysLogInfoRepository;
import com.ruge.system.pojo.request.RequestLoginfo;
import com.ruge.system.service.SysLogInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.List;
import java.util.Map;

/**
 * @author ruge.wu
 * @program ruge
 * @description 系统登录日志 逻辑层
 * @date 2020-03-10 16:27
 **/
@Service
public class SysLogInfoServiceImpl implements SysLogInfoService {
    @Resource
    private SysLogInfoRepository sysLogInfoRepository;

    /**
     * 数据查询接口
     *
     * @param RequestLoginfo SysLogLogin
     * @return Page<SysLogLogin>
     */
    @Override
    public Page<SysLogInfo> findAll(RequestLoginfo loginfo) {
        Specification specification = (Specification<SysMenu>) (root, query, cb) -> {
            /**
             * 获取条件对象
             */
            Predicate conjunction = cb.conjunction();
            if (loginfo != null) {
               if (StringUtils.isNotBlank(loginfo.getUserName())) {
                    conjunction.getExpressions().add(cb.like(root.get("userName"), "%" + loginfo.getUserName() + "%"));
                }
                if (StringUtils.isNotBlank(loginfo.getIp())) {
                    conjunction.getExpressions().add(cb.like(root.get("ip"), "%" + loginfo.getIp() + "%"));
                }

                if (null!=loginfo.getStartTime()) {
                    conjunction.getExpressions().add(cb.greaterThanOrEqualTo(root.get("startTime"), loginfo.getStartTime()));
                }
                if (null!=loginfo.getEndTime()) {
                    conjunction.getExpressions().add(cb.lessThanOrEqualTo(root.get("endTime"), loginfo.getEndTime()));
                }
            }
            return conjunction;
        };
        Pageable pageable;
        if (loginfo == null) {
            pageable = PageRequest.of(0, 10);
        } else {
            pageable = PageRequest.of(loginfo.getPage(), loginfo.getLimit());
        }
        return sysLogInfoRepository.findAll(specification, pageable);

    }

    /**
     * 数据保存接口
     *
     * @param sysLogLogin SysLogLogin
     * @return SysLogLogin
     */
    @Override
    public SysLogInfo save(SysLogInfo sysLogLogin) {
        return sysLogInfoRepository.save(sysLogLogin);
    }

    /**
     * 数据删除接口
     *
     * @param id id
     */
    @Override
    public void deleteById(Long id) {
        sysLogInfoRepository.deleteById(id);
    }

    /**
     * 批量操作接口
     *
     * @param map Map<String, List<SysLogLogin>> map
     * @return List<SysLogLogin>
     */
    @Override
    public List<SysLogInfo> batch(Map<String, List<SysLogInfo>> map) {
        return sysLogInfoRepository.batch(map);
    }
}
